<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="username">
        <el-input
            v-model="queryParams.username"
            placeholder="用户名、姓名、手机号码"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
            v-model="dateRange"
            type="daterange"
            value-format="YYYY-MM-DD"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['user:userInfo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userInfoList">
      <el-table-column label="用户名" align="center" prop="username"/>
      <el-table-column label="昵称" align="center" prop="nickName"/>
      <el-table-column label="头像" align="center" prop="avatar" #default="scope">
        <img :src="scope.row.avatar" width="50"/>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex" #default="scope">
        {{ scope.row.sex == 1 ? '女' : '男' }}
      </el-table-column>
      <el-table-column label="电话号码" align="center" prop="phone"/>
      <el-table-column label="最后一次登录时间" align="center" prop="lastLoginTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastLoginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" #default="scope">
        {{ scope.row.status == 1 ? '正常' : '停用' }}
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleShow(scope.row)"
                     v-hasPermi="['user:userInfo:query']">详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改会员对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" append-to-body>
      <el-form ref="userInfoRef" :model="form" label-width="80px">
        <el-divider/>
        <span style="margin-bottom: 5px;">基本信息</span>
        <el-row>
          <el-col :span="12">
            <el-form-item label="头像" label-width="100px">
              <img :src="form.avatar" width="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名" label-width="100px">
              {{ form.username }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="昵称" label-width="100px">
              {{ form.nickName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" label-width="100px">
              {{ form.sex == 1 ? '女' : '男' }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号码" label-width="100px">
              {{ form.phone }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" label-width="100px">
              {{ form.memo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最近登录ip" label-width="100px">
              {{ form.lastLoginIp }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最近登录时间" label-width="100px">
              {{ form.lastLoginTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" label-width="100px">
              {{ form.status == 1 ? '正常' : '停用' }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" label-width="100px">
              {{ form.createTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider/>
        <span style="margin-bottom: 5px;">地址信息</span>
        <el-table :data="userAddressList" style="width: 100%">
          <el-table-column prop="tagName" label="地址标签" width="80"/>
          <el-table-column
              prop="isDefault"
              label="是否默认"
              #default="scope"
              width="100"
          >
            {{ scope.row.isDefault == 1 ? '是' : '否' }}
          </el-table-column>
          <el-table-column prop="isDefault" label="详细详细" #default="scope">
            {{ scope.row.name }} | {{ scope.row.phone }} |
            {{ scope.row.fullAddress }}
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="160"/>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="UserInfo">
import {listUserInfo, getUserAddress} from "@/api/user/userInfo";

const {proxy} = getCurrentInstance();

const userInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    username: null,
    createTimeBegin: '',
    createTimeEnd: ''
  }
});

const {queryParams, form} = toRefs(data);

/** 查询会员列表 */
function getList() {
  loading.value = true;
  listUserInfo(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    userInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  form.value = {}
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 查看会员详情 */
const userAddressList = ref([])

function handleShow(row) {
  form.value = row;
  open.value = true;
  title.value = "查看会员";
  getUserAddress(row.id).then(res => {
    userAddressList.value = res.data
  })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('user/userInfo/export', {
    ...queryParams.value
  }, `userInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
